﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Logic.Functions;

namespace Logic
{
    public class Layer : BaseLayer
    {
        public Neuron[] Neurons { get; set; }

        public override string Symbol
        {
            get { return "LinearLayer"; }
        }

        public override void ProcessNeurons(float[] prevLayerSignals)
        {
            foreach (Neuron n in Neurons)
                n.Evaluate(ActivationFunction, prevLayerSignals);
        }

        public override float[] GetOutputSignals()
        {
            var outputSignals = new float[Neurons.Length];
            for (int i=0; i<Neurons.Length; i++)
                outputSignals[i] = Neurons[i].OutputSignal;
            return outputSignals;
        }

        public override void Save(StreamWriter sw)
        {
            sw.WriteLine(Symbol);
            sw.WriteLine("{0} {1}", Neurons.Length, ActivationFunction.Symbol);
            foreach (Neuron neuron in Neurons)
            {
                foreach (float w in neuron.Weights)
                    sw.Write("{0} ", w);
                sw.WriteLine(neuron.Bias);
            }
        }

        public override void CleanWeights()
        {
            foreach (Neuron n in Neurons)
                n.CleanWeights();
        }

        public override void InitializeWeights(float min, float max)
        {
            foreach (Neuron n in Neurons)
                n.InitializeWeights(min, max);
        }

        public override void PrintWeights()
        {
            foreach (Neuron neuron in Neurons)
            {
                foreach (float w in neuron.Weights)
                    Console.Write("{0} ", w);
                Console.WriteLine();
            }
        }

        public override int Size
        {
            get { return Neurons.Length; }
        }
    }
}
